package net.sevear.chaos.view.recommend
{
	import com.douban.media.DBRadio;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	
	import net.sevear.chaos.douban.DBShuoManager;
	import net.sevear.chaos.douban.NeedUpdatedPlayList;
	import net.sevear.chaos.event.FanRollEvent;
	import net.sevear.chaos.event.RecommendEvent;
	import net.sevear.chaos.recommend.RecommendManager;
	import net.sevear.chaos.view.FanRoller;
	
	import spark.components.Group;
	
	public class FanRotator extends Group
	{
		private var _fan:FanRoller;
		private var _selectedIDs:Array;
		private var _colors:Array;
		public function FanRotator(size:int)
		{
			super();
			this.width = this.height = size;
			_fan = new FanRoller(size,size);
			_fan.enabled = false;
			_fan.addEventListener(FanRollEvent.PASSING,onPassing);
			_fan.addEventListener(FanRollEvent.DRAG_OVER,onRelease);
//			_fan.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
//			_fan.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
			_fan.addEventListener(FanRollEvent.ROLLING_EFFECT_END,onRollingEnd);
			var recommend:RecommendManager = RecommendManager.getInstance();
			recommend.addEventListener(RecommendEvent.GET_NEW_RECOMMENDLIST,onGetNewRes);
			recommend.addEventListener(RecommendEvent.SEND_A_RECOMMENDATION_SUCCESSFULLY,onSendReSuccesfully);
			this.addElement(_fan);
		}
		
		
		public function setSelectedIDs(value:Array):void{
			if(value!=null){
				_selectedIDs = value;
				_fan.enable = true;
				_fan.setInterval(value.length);
			}else{
				_fan.enable = false;
			}
		}
		
		private var _circleCount:int;
		private function onPassing(e:FanRollEvent):void{
//			if(contacts.length>e.idx){
//				var user:UserInfo = contacts[e.idx] as UserInfo;
//				trace("passing :",user.dbName);
//				if(user!=null){
//					if(selectedIDs.source.indexOf(user.dbUID)>=0){
//						deleteSelected(user);
//					}else{
//						addSelect(user);
//					}
//					updateSelectedNames();
//					fanView.setSelectionStyle(e.idx,user.selected);
//				}
//			}
			if(e.idx==0){
				_circleCount ++;
				trace("after passing current circle count:",_circleCount);
			}
		}
		
//		private function onMouseOver(e:MouseEvent):void{
////			contactContainer.visible = true;
//		}
//		
//		private function onMouseOut(e:MouseEvent):void{
////			contactContainer.visible = false;
//		}
		
		private function onRelease(e:FanRollEvent):void{
			_fan.startRolling(Math.max(_circleCount,1));
		}
		
		private function onRollingEnd(e:FanRollEvent):void{
			if(_selectedIDs && _selectedIDs.length>0){
				_circleCount = 0;
				_fan.enable = false;
				_fan.toolTip = "发送推荐中...";
				RecommendManager.getInstance().recommendToList(DBRadio.getInstance().curSong,_selectedIDs);
			}
			
			dispatchEvent(new FanRollEvent(FanRollEvent.ROLLING_EFFECT_END));
		}
		
		private function onGetNewRes(e:RecommendEvent):void{
			_fan.startRolling((e.data as ArrayCollection).length,true);
		}
		
		private function onSendReSuccesfully(e:RecommendEvent):void{
			_fan.enable = true;	
			_fan.toolTip = "推荐吧";
		}
	}
}